import random
import timeit

from sort.sort import selection_sort, insertion_sort, merge_sort, merge_sort_b2u, quick_sort


def main1():
    l1 = [random.randint(0, 10000) for _ in range(10000)]
    l2 = list(l1)
    l3 = list(l1)
    code1 = """selection_sort(l1)"""
    code2 = """insertion_sort(l2)"""
    code3 = """merge_sort(l3)"""
    t1 = timeit.timeit(stmt=code1, number=3, setup="from __main__ import selection_sort",
                       globals={"l1": l1, "l2": l2, "l3": l3})
    t2 = timeit.timeit(stmt=code2, number=3, setup="from __main__ import insertion_sort",
                       globals={"l1": l1, "l2": l2, "l3": l3})
    t3 = timeit.timeit(stmt=code3, number=3, setup="from __main__ import merge_sort",
                       globals={"l1": l1, "l2": l2, "l3": l3})
    print(f"selection_sort: {t1}, insertion_sort: {t2}, merge_sort: {t3}")


def main2():
    l1 = [6, 1, 7, 3, 0, 5, 4, 5, 8, 2, 9]
    l2 = [x for x in range(100)]
    l3 = list(reversed(l2))

    print(f"""
    l1: {l1}
    l2: {l2}
    l3: {l3}""")
    print("merge sort")
    merge_sort(l1)
    merge_sort(l2)
    merge_sort(l3)
    print(f"""
        l1: {l1}
        l2: {l2}
        l3: {l3}""")


def main3():
    l1 = [random.randint(0, 100) for _ in range(1000000)]
    l2 = [x for x in range(100000)]
    l3 = list(l1)
    l4 = list(l1)
    l5 = list(l1)
    code1 = """merge_sort(l1)"""
    code2 = """merge_sort(l2)"""
    code4 = """merge_sort_b2u(l4)"""
    code5 = """quick_sort(l5)"""
    t1 = timeit.timeit(stmt=code1, number=5, setup="from __main__ import merge_sort",
                       globals={"l1": l1})
    t2 = timeit.timeit(stmt=code2, number=5, setup="from __main__ import merge_sort",
                       globals={"l2": l2})
    t3 = timeit.timeit(stmt="l3.sort()", number=5, globals={"l3": l3})

    # t4 = timeit.timeit(stmt=code4, number=5, setup="from __main__ import merge_sort_b2u",
    #                    globals={"l4": l4})
    t5 = timeit.timeit(stmt=code5, number=5, setup="from __main__ import quick_sort",
                       globals={"l5": l5})
    print(f"merge_sort: {t1}, {t2}, {t3}, {t5}", )


def test_quick_sort():
    # a = [3, 4, 5, 2, 1, 0, -3, 9, 11, -2]
    a = list(random.randint(0, 100000) for _ in range(100000))
    b = list(a)
    quick_sort(a)
    b.sort()
    print(a == b)


if __name__ == "__main__":
    # main3()
    test_quick_sort()
